import com.alibaba.fastjson.JSONObject;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.TbkCouponGetRequest;
import com.taobao.api.request.TbkDgMaterialOptionalRequest;
import com.taobao.api.request.TbkTpwdCreateRequest;
import com.taobao.api.response.TbkCouponGetResponse;
import com.taobao.api.response.TbkDgMaterialOptionalResponse;
import com.taobao.api.response.TbkTpwdCreateResponse;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author xianqiao.gu
 * @title: Test
 * @projectName admin
 * @description: TODO
 * @date 2020/4/29 0029下午 3:35
 */
public class Test {
    private static String url="http://gw.api.taobao.com/router/rest";
    private static String appkey="29423169";
    private static String secret="2178e28513e44a92f1999aaf56055a89";
    public static void main(String[] args) {
//        SpringApplication.run(Test.class, args);
            Test t=new Test();
//        t.parseTaokoulin("$owLB1NNeI4D$");
        try {
            t.getP();
//            t.test2();
        } catch (ApiException e) {
            e.printStackTrace();
        }
    }
    private void getYouhui(Long itemId, String couponId)throws  ApiException{
        TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
        TbkCouponGetRequest req = new TbkCouponGetRequest();
//        req.setMe("nfr%2BYTo2k1PX18gaNN%2BIPkIG2PadNYbBnwEsv6mRavWieOoOE3L9OdmbDSSyHbGxBAXjHpLKvZbL1320ML%2BCF5FRtW7N7yJ056Lgym4X01A%3D");
//        req.setMe("nfr%2BYTo2k1PX18gaNN%2BIPkIG2PadNYbBnwEsv6mRavWieOoOE3L9OdmbDSSyHbGxBAXjHpLKvZbL1320ML%2BCF5FRtW7N7yJ056Lgym4X01A%3D");
        req.setItemId(itemId);
        req.setActivityId(couponId);
        TbkCouponGetResponse rsp = client.execute(req);
        System.out.println(rsp.getBody());
    }

    /**
    　　* @description:
    　　* @param ${tags}
    　　* @return ${return_type}
    　　* @throws
    　　* @author xianqiao.gu
    　　* @date 2020/4/29 0029 下午 5:09
    　　*/
    private List<TbkDgMaterialOptionalResponse.MapData> getP() throws ApiException {
        TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
        TbkDgMaterialOptionalRequest req = new TbkDgMaterialOptionalRequest();
        req.setQ("南极人 冰丝防晒套袖男士夏季冰爽袖套薄长款护臂女开车手套袖子");
        req.setAdzoneId(110323100266L);
        TbkDgMaterialOptionalResponse rsp = client.execute(req);
        for(TbkDgMaterialOptionalResponse.MapData data:rsp.getResultList()){
            if(!StringUtils.isEmpty(data.getCouponId())){
                System.out.println(data.getShopTitle());
                System.out.println(JSONObject.toJSON(data));
                this.getYouhui(data.getItemId(),data.getCouponId());
                this.tpwdCreate(data.getCouponShareUrl());
                this.tpwdCreate(data.getItemUrl());
            }
        }
        return rsp==null?null:rsp.getResultList();
    }
    private void tpwdCreate(String itemUrl) throws ApiException {
        TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
        TbkTpwdCreateRequest req = new TbkTpwdCreateRequest();
//        req.setUserId("123");
        req.setText("长度大于5个字符");
        if(!itemUrl.startsWith("https:")){
            itemUrl="https:"+itemUrl;
        }
        req.setUrl(itemUrl);
        System.out.println(req.getUrl());
//        req.setLogo("https://uland.taobao.com/");
//        req.setExt("{das:dsa}");
        TbkTpwdCreateResponse rsp = client.execute(req);
        System.out.println(rsp.getBody());
        this.tbkClickExtract(rsp.getData().getModel());
    }
//    private  void test2() throws ApiException {
//        TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
//        TbkTpwdCreateRequest req = new TbkTpwdCreateRequest();
//        req.setText("长度大于5个字符");
//        req.setUrl("https://uland.taobao.com/coupon/edetail?e=9o9VINpfFy0NfLV8niU3RxrSI%2FOabn6qNg4Gqf8CT4A24f0SZl4gles6ZzgjVHIemMHpNfYdHdCKOA022ZL%2BJJnuVO9s8v%2Fti1pMTUcPySdVRwek%2BFS4Tz39Nh41%2BmYftVdeSQ8%2Bxhy6SvogdHxHZL5enAZ8osjqiKaTvEN98B77BAkxM6e3IKlpoIITcB761LeEfNPJT5MM1L%2FmEra8u1kT%2Burs3qO2&&app_pvid=59590_11.186.132.163_22299_1588208501043&ptl=floorId:2836;app_pvid:59590_11.186.132.163_22299_1588208501043;tpp_pvid:100_11.12.63.68_98309_3091588208501047847&xId=TgCN03c9sPypzXzXrXppcSEkx23qpSQPwbOc7ITCfQ7hvLUNeYHtdVCtp4Dr8LXolYpYf138FA6qPd5GpDWhxZCdHI1LUar0vUyiNQjihdM&union_lens=lensId%3AMAPI%401588208501%400bba84a3_0e8f_171c89a8171_a91c%4001");
//        TbkTpwdCreateResponse response = client.execute(req);
//        System.out.println(response.getBody());
//
//    }
    private void tbkClickExtract(String clickUrl){
        String url="https://api.taokouling.com/tkl/tkljm";
        clickUrl="$owLB1NNeI4D$";
        String res=HttpUtils.sendPost(url,"apikey=JvcvMWTKUJ&tkl="+clickUrl);
        TklRes tklRes=JSONObject.parseObject(res, TklRes.class);
        System.out.println(tklRes.getUrl());
    }

}
